package com.study_platform.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study_platform.entity.Question;
import com.study_platform.entity.TestPaper;
import com.study_platform.mapper.TestPaperMapper;
import com.study_platform.service.QuestionService;
import com.study_platform.service.TestPaperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auto
 * @since 2023-04-11
 */
@Service
@Transactional
public class TestPaperServiceImpl extends ServiceImpl<TestPaperMapper, TestPaper> implements TestPaperService {
    @Resource
    private QuestionService questionService;
    @Override
    public Integer getTestId() {//获取考试场次id
        Integer ans = 1;
        List<TestPaper> list = this.lambdaQuery()
                .orderByDesc(TestPaper::getTestId)
                .page(new Page<>(1, 10))
                .getRecords();
        if (list.size() > 0) {
            ans = list.get(list.size() - 1).getTestId() + 1;
        }
        return ans;
    }

    @Override
    public List<Question> createPaper(String StringGrade, String point, Integer testId, String StringSize){
        Integer size = Integer.valueOf(StringSize);
        Integer grade = Integer.valueOf(StringGrade);
        List<Question> questionList = questionService.list();
        if (grade < 7) {
            grade = 7;
        }
        else if (grade > 9) {
            grade = 9;
        }
        Integer finalGrade = grade;
        questionList.removeIf(question -> !question.getGrade().equals(finalGrade));
        questionList.removeIf(question -> !question.getPoint().equals(point));
        Random rand = new Random();
        while (questionList.size() > size) {
            questionList.remove(rand.nextInt(questionList.size()));
        }
        for (Question question : questionList) {
            TestPaper testPaper = new TestPaper();
            testPaper.setTestId(testId);
            testPaper.setQuestionId(question.getQuestionId());
            this.saveOrUpdate(testPaper);
        }
        return questionList;
    }

    @Override
    public JSONObject createTestPaper(String StringGrade, String point, Integer testId, String StringSize) {
        JSONObject jsonObject = new JSONObject();
        Integer size = Integer.valueOf(StringSize);
        Integer grade = Integer.valueOf(StringGrade);
        List<Question> questionList = questionService.list();
        if (grade < 7) {
            grade = 7;
        }
        else if (grade > 9) {
            grade = 9;
        }
        Integer finalGrade = grade;
        questionList.removeIf(question -> !question.getGrade().equals(finalGrade));
        questionList.removeIf(question -> !question.getPoint().equals(point));
        Random rand = new Random();
        while (questionList.size() > size) {
            questionList.remove(rand.nextInt(questionList.size()));
        }
        for (Question question : questionList) {
            TestPaper testPaper = new TestPaper();
            testPaper.setTestId(testId);
            testPaper.setQuestionId(question.getQuestionId());
            this.saveOrUpdate(testPaper);
        }
        jsonObject.put("test_id", testId);
        jsonObject.put("questions", questionList);
        return jsonObject;
    }
}
